// *** LICENSE HEADER ***
// Filename: ./colorselect.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "colorselect.h"

GLubyte colorselect::playerColors[8][3] = {
    {0, 0, 255}, //First player default color
    {255, 0, 0}, //Second player default color
    {0, 255, 0}, //etc...
    {255, 255, 0},
    {255, 0, 255},
    {0, 255, 255},
    {255, 255, 255},
    {128, 128, 255}
};

colorselect::colorselect(int newPosX, int newPosY, int selection, int newId):
    guielement(newPosX, newPosY, "", newId){
    currentSelection = selection;

    Constrainti(currentSelection, 0, 7);
}

colorselect::~colorselect() {}

int colorselect::Click(int clickX, int clickY) {
    if (clickX  >= x && clickX < x + 160 && clickY >= y && clickY < y + 20)
        return id;
    else return 0;
}

int colorselect::Release(int clickX, int clickY) {
    if (active && clickX  >= x && clickX < x + 160 && clickY >= y && clickY < y + 20){
        currentSelection = (clickX - x) / 20;
        Constrainti(currentSelection, 0, 7);
        return id;
    }
    else return 0;
}

void colorselect::GetColors(GLubyte& red, GLubyte& green, GLubyte& blue){
    red = playerColors[currentSelection][0];
    green = playerColors[currentSelection][1];
    blue = playerColors[currentSelection][2];
}

void colorselect::Draw(fonts* fontDrawer){
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_TEXTURE_2D);

    for (int i = 0; i < 8; ++i){
        //Draw the color rectangles
        if (i == currentSelection) {
            glBegin(GL_TRIANGLES);
            glColor3ub(playerColors[i][0], playerColors[i][1], playerColors[i][2]);
            glVertex2i(x+i*20, _HEIGHT-y);
            glVertex2i(x+i*20, _HEIGHT-y-20);
            glVertex2i(x+20+i*20, _HEIGHT-y-20);
            glVertex2i(x+20+i*20, _HEIGHT-y-20);
            glVertex2i(x+20+i*20, _HEIGHT-y);
            glVertex2i(x+i*20, _HEIGHT-y);
            glEnd();
        }

        //Draw the borders
        if (i != currentSelection) {
            glBegin(GL_LINE_STRIP);
            glColor3ub(playerColors[i][0], playerColors[i][1], playerColors[i][2]);
            glVertex2i(x+2+i*20, _HEIGHT-y-2);
            glVertex2i(x+2+i*20, _HEIGHT-y-18);
            glVertex2i(x+18+i*20, _HEIGHT-y-18);
            glVertex2i(x+18+i*20, _HEIGHT-y-2);
            glVertex2i(x+2+i*20, _HEIGHT-y-2);
            glEnd();
        }
    }
}
